<template>
	<view class="grab-order-details page-flex">
		<view class="content">
			<uv-skeleton :loading="loading" rows="4">
				<view class="box info">
					<view class="top">
						<view class="name">{{ details.job_type_id[0] && details.job_type_id[0].name }}</view>
						<view class="distance">距您{{ details.distance.km }}公里</view>
					</view>
					<view class="center">
						<view class="date">
							<uv-text
								prefix-icon="more-circle-fill"
								:icon-style="{ marginRight: '10px' }"
								:text="dayjs(details.employment_time).format('YYYY-MM-DD')"
							></uv-text>
						</view>
						<view class="dot address">
							<uv-text :text="details.address" lines="1"></uv-text>
						</view>
						<view class="dot house_number">
							<uv-text :text="details.house_number" lines="1"></uv-text>
						</view>
					</view>
				</view>
			</uv-skeleton>
			<uv-skeleton :loading="loading" rows="1">
				<view class="box offer">
					<view class="label">客户报价</view>
					<view class="amount">
						<text class="unit">￥</text>
						<text class="number">{{ details.offer }}</text>
					</view>
				</view>
			</uv-skeleton>
			<uv-skeleton :loading="loading" rows="4">
				<view class="box date">
					<view class="field">
						<view class="label">服务人数：</view>
						<view class="value success">{{ details.male_numbers + details.female_numbers }}人</view>
					</view>
					<view class="field">
						<view class="label">收费模式：</view>
						<view class="value success">
							{{ chargingModeMap[details.charging_mode] }}
						</view>
					</view>
					<view class="field">
						<view class="label">订单备注：</view>
						<view class="value success">{{ details.supplement }}</view>
					</view>
				</view>
			</uv-skeleton>
		</view>
		<view class="buttons">
			<view class="button">
				<uv-button text="导航" type="success" shape="circle" @click="handleChooseLocation"></uv-button>
			</view>
			<view class="button">
				<uv-button text="拨打电话" type="primary" shape="circle" @click="handleCall"></uv-button>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onShow } from "@dcloudio/uni-app";
import { ref } from "vue";
import dayjs from "dayjs";
import getWaitTime from "../../utils/get-wait-time";
import getDistances from "../../utils/get-distances";
import { useUserStore } from "../../store/user";

const uniIdCo = uniCloud.importObject("uni-id-co");

const db = uniCloud.database();

const userStore = useUserStore();

const props = defineProps({
	orderId: String
});

const chargingModeMap = {
	0: "自己出价",
	1: "计时"
};

const loading = ref(true);

const locationInfo = ref({});

const details = ref({ job_type_id: [], distance: {} });

const handleGetOrderDetails = async (orderId) => {
	loading.value = true;
	const order = db.collection("order").where(`_id == "${orderId}"`).getTemp();
	const response = await db.collection(order, "job-type").get();
	if (response.result && response.result.errCode == 0 && response.result.data[0]) {
		const result = response.result.data[0];
		result.distance = getDistances(
			locationInfo.value.latitude,
			locationInfo.value.longitude,
			result.latitude,
			result.longitude
		);
		console.log(result);
		details.value = result;
		loading.value = false;
	} else {
		uni.showModal({
			title: "提示",
			content: "此订单不存在",
			showCancel: false,
			confirmText: "返回",
			success: (result) => {
				if (result.confirm) {
					uni.navigateBack();
				}
			}
		});
	}
};

// 获取坐标
const handleGetLocation = async () => {
	return new Promise((resolve) => {
		// #ifdef APP || MP-WEIXIN
		uni.getLocation({
			type: "wgs84",
			isHighAccuracy: true,
			success: (result) => {
				locationInfo.value = result;
				resolve(result);
			}
		});
		// #endif
		// #ifdef H5
		resolve();
		// #endif
	});
};

const handleChooseLocation = () => {
	uni.openLocation({
		longitude: details.value.longitude,
		latitude: details.value.latitude
	});
};

const handleCall = async (order) => {
	if (userStore.accountInfo.isMobileBound) {
		const ressponse = await uniIdCo.callOverseer({ overseerId: details.value.user_id });
		if (ressponse.errCode == 0) {
			uni.makePhoneCall({
				phoneNumber: ressponse.mobile,
				success: (result) => {
					console.log(result);
				}
			});
		}
	} else {
		uni.showModal({
			title: "提示",
			content: "未绑定手机号",
			confirmText: "去绑定",
			success: (result) => {
				if (result.confirm) {
					uni.navigateTo({
						url: "/uni_modules/uni-id-pages/pages/userinfo/bind-mobile/bind-mobile"
					});
				}
			}
		});
	}
};

onShow(async () => {
	await handleGetLocation();
	await handleGetOrderDetails(props.orderId);
});
</script>

<style lang="scss" scoped>
.grab-order-details {
	box-sizing: border-box;
	background-color: #fafafa;

	.content {
		flex: 1;
		padding: 20px;
		.box {
			background-color: #ffffff;
			padding: 10px;
			border-radius: 10px;
			box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.1);
			font-weight: bold;

			&.info {
				background-color: #ffffff;
				margin-bottom: 20px;
				border-radius: 10px;
				padding: 20px;
				box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.1);

				.top {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 10px;
				}

				.center {
					margin-bottom: 10px;

					.date {
						margin-bottom: 5px;
					}

					.dot {
						display: flex;
						align-items: center;
						margin-bottom: 5px;

						&:before {
							content: "";
							display: block;
							width: 15px;
							height: 15px;
							border-radius: 50%;
							margin-right: 10px;
						}

						&.name {
							&:before {
								background-color: #33cc66;
							}
						}

						&.address {
							&:before {
								background-color: #e50f0f;
							}
						}
					}
				}
			}

			&.offer {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 20px;

				.amount {
					color: #e91e1e;
					.unit {
						font-size: 12px;
					}
					.number {
						font-size: 18px;
					}
				}
			}

			&.date {
				.field {
					display: flex;
					align-items: center;
					margin-bottom: 10px;

					.success {
						color: #33cc66;
					}
				}
			}
		}
	}

	.buttons {
		padding: 20px;
		background-color: #ffffff;
		display: flex;
		gap: 20px;

		.button {
			flex: 1;
		}
	}
}
</style>
